package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.commondata.world.WorldConstant;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class WorldUserData extends WorldBaseData {
    public static UserWorldInfo getUserWorldInfo(long userId) {
        return (UserWorldInfo) EntityManager.getUniqueEntity(UserWorldInfo.class, userId);
    }

    public static UserWorldEventLocation getUserWorldEventLocation(long userId, int locationId) {
        return (UserWorldEventLocation) EntityManager.getEntity(UserWorldEventLocation.class, userId, locationId);
    }

    // 获取UserWorldEventLocation列表
    public static List<UserWorldEventLocation> getUserWorldEventLocationList(long userId) {
        List<UserWorldEventLocation> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserWorldEventLocation.class, userId)) {
            UserWorldEventLocation entity = (UserWorldEventLocation) model;
            list.add(entity);
        }
        return list;
    }

    public static UserWorldPartner getUserWorldPartner(long userId, int partnerId) {
        UserWorldPartner userWorldPartner = (UserWorldPartner) EntityManager.getEntity(UserWorldPartner.class, userId, partnerId);
        if (userWorldPartner == null) {
            userWorldPartner = new UserWorldPartner();
            userWorldPartner.putUserId(userId);
            userWorldPartner.putPartnerId(partnerId);
            userWorldPartner.doCreate();
        }
        return userWorldPartner;
    }

    // 获取UserWorldPartner列表
    public static List<UserWorldPartner> getUserWorldPartnerList(long userId) {
        List<UserWorldPartner> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserWorldPartner.class, userId)) {
            UserWorldPartner entity = (UserWorldPartner) model;
            list.add(entity);
        }
        return list;
    }

    public static UserWorldTeam getUserWorldTeam(long userId, int teamId) {
        return (UserWorldTeam) EntityManager.getEntity(UserWorldTeam.class, userId, teamId);
    }

    // 获取UserWorldTeam列表
    public static List<UserWorldTeam> getUserWorldTeamList(long userId) {
        List<UserWorldTeam> list = new ArrayList<>();
        int count = 0;
        for (Model model : EntityManager.getEntityList(UserWorldTeam.class, userId)) {
            UserWorldTeam entity = (UserWorldTeam) model;
            if (!entity.isTmp()) {
                count++;
                list.add(entity);
            } else {
                if (count >= AllParam.WORLD_MARCH_NUM && entity.getStatus() == WorldConstant.WORLD_TEAM_STATUS_0) {// 永久队伍的数量达到上限，且临时队伍空闲状态
                    entity.delete();
                } else {
                    list.add(entity);
                }
            }
        }
        return list;
    }

    public static UserWorldEventQueue getUserWorldEventQueue(long userId, long dbid) {
        return (UserWorldEventQueue) EntityManager.getEntity(UserWorldEventQueue.class, userId, dbid);
    }

    // 获取UserWorldEventQueue列表
    public static List<UserWorldEventQueue> getUserWorldEventQueue(long userId) {
        List<UserWorldEventQueue> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserWorldEventQueue.class, userId)) {
            UserWorldEventQueue entity = (UserWorldEventQueue) model;
            if (entity.isExpire()) {
                entity.delete();
            } else {
                list.add(entity);
            }
        }
        return list;
    }


    public static UserWorldRecord getUserWorldRecord(long userId, long dbid) {
        return (UserWorldRecord) EntityManager.getEntity(UserWorldRecord.class, userId, dbid);
    }

    // 获取UserWorldRecord列表
    public static List<UserWorldRecord> getUserWorldRecordList(long userId) {
        List<UserWorldRecord> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserWorldRecord.class, userId)) {
            UserWorldRecord entity = (UserWorldRecord) model;
            list.add(entity);
        }
        return list;
    }

    public static UserWorldEventLocationTeam getUserWorldEventLocationTeam(long userId, int locationId, int teamId) {
        String uniqueId = WorldManager.getLocationUnique(userId, locationId);
        return (UserWorldEventLocationTeam) EntityManager.getEntity(UserWorldEventLocationTeam.class, uniqueId, teamId);
    }

    public static List<UserWorldEventLocationTeam> getUserWorldEventLocationTeamList(long userId, int locationId) {
        String uniqueId = WorldManager.getLocationUnique(userId, locationId);
        List<UserWorldEventLocationTeam> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserWorldEventLocationTeam.class, uniqueId)) {
            UserWorldEventLocationTeam entity = (UserWorldEventLocationTeam) model;
            list.add(entity);
        }
        return list;
    }

    // 获取紧急事件的坑位列表
    public static List<UserWorldEventLocation> getUserWorldEventLocationListUrgent(long userId, UserWorldInfo userWorldInfo) {
        JsonArray jsonArray = new JsonArray();
        for (int i=0; i<userWorldInfo.getEventLocationNumUrgent(); i++) {
            int locationId = WorldConstant.WORLD_EVENT_LOCATION_ID_URGENT + i;
            jsonArray.add(locationId);
        }
        List<UserWorldEventLocation> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserWorldEventLocation.class, userId, jsonArray)) {
            UserWorldEventLocation entity = (UserWorldEventLocation) model;
            list.add(entity);
        }
        return list;
    }
}
